# -*- coding: utf-8 -*-
# /home/slam-dunk/anaconda3/envs/ros/bin/python


import numpy as np
import matplotlib.pyplot as plt
import math
def show(x1:list,x2:list,format:str):
    for i in range(len(x1)):
        plt.plot(x1,x2,format)



def PDA(x0:np.ndarray,x1:np.ndarray):
    '''
    :param x0:2*n ,
    :param x1:2*m,
    :return:
    '''
    #shape 2*1
    mu_0=np.mean(x0,axis=1).reshape((-1,1))
    mu_1=np.mean(x1,axis=1).reshape((-1,1))
    #shape 2*2
    signm_0=(x0-mu_0).dot((x0-mu_0).T)
    signm_1 = (x1 - mu_1).dot((x1 - mu_1).T)
    sw=signm_0+signm_1
    print(np.linalg.inv(sw),'\n',x0,'\n',mu_0,'\n',x1,'\n')
    w=np.linalg.inv(sw).dot(mu_0-mu_1)
    return w



'''
the data for the first category
'''
x0_1=[]#midu
x0_2=[]#tiandu
y0=[]
'''
the data for the second category
'''
x1_1=[]#midu
x1_2=[]#tiandu
y1=[]
with open('3.3.txt',mode='r') as file:
    for line in file:
        data=line.strip().split(',')
        if data[3]=='1':
            x0_1.append(float(data[1]))
            x0_2.append(float(data[2]))
            y0.append(float(data[3]))
        else:
            x1_1.append(float(data[1]))
            x1_2.append(float(data[2]))
            y1.append(float(data[3]))
#shape 2*n
x0=np.array(x0_1+x0_2).reshape((2,-1))
#shape 2*m
x1=np.array(x1_1+x1_2).reshape((2,-1))
plt.subplot(1,2,1)
show(x0_1,x0_2,'+')
show(x1_1,x1_2,'o')
w=PDA(x0,x1)
print(w)
plt.plot([0,0.1],[0,w[1,0]*0.1/w[0,0]],'y-')
plt.xlabel('midu')
plt.ylabel('tiandu')

x0_reflet=np.dot(w.T,x0).reshape(-1)
x1_reflect=np.dot(w.T,x1).reshape(-1)
cos=w[0,0]/math.sqrt(w[0,0]**2+w[1,0]**2)
sin=w[1,0]/math.sqrt(w[0,0]**2+w[1,0]**2)
plt.subplot(1,2,2)
show(x0_1,x0_2,'+')
show(x1_1,x1_2,'o')
plt.scatter(x0_reflet*cos,x0_reflet*sin,c='r',s=10,marker='p')
plt.scatter(x1_reflect*cos,x1_reflect*sin,c='b',s=10)
plt.show()
